package fr.pioug.gdocs.object;

import java.io.*;
import java.util.*;

import org.apache.commons.lang.*;
import org.apache.commons.lang.builder.*;

import com.google.gdata.data.*;
import com.google.gdata.data.docs.*;

/**
 * @author a011228
 * @version
 */
public class GDocsFile {

	private String mName;

	private String mPath;

	private DocumentListEntry mEntryServer;

	private String mAbsolutePath;

	private Date mLastModified;

	/**
	 * @author a011228
	 * @version
	 */
	public static enum DiffType {
		EXIST, DELETE_SERVER, DELETE_LOCAL, UPDATE_LOCAL, UPDATE_SERVER, NOT_EXIST_LOCAL, NOT_EXIST_SERVER
	}

	/**
	 */
	protected DiffType mDiffType;

	/**
	 */
	public static final String FOLDER_CATEGORY = "folder";

	private boolean mIsDirectory;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Copy Constructor
	 * 
	 * @param gDocsFile
	 *            a <code>GDocsFile</code> object
	 */
	public GDocsFile(GDocsFile gDocsFile) {
		this.mName = gDocsFile.mName;
		this.mPath = gDocsFile.mPath;
		this.mEntryServer = gDocsFile.mEntryServer;
		this.mAbsolutePath = gDocsFile.mAbsolutePath;
		this.mLastModified = gDocsFile.mLastModified;
		this.mDiffType = gDocsFile.mDiffType;
		this.mIsDirectory = gDocsFile.mIsDirectory;
	}

	/**
	 * @param pDirectory
	 * @param pTreeRoot
	 * @return
	 */
	public GDocsFile(File pDirectory, boolean pTreeRoot) {
		mName = pDirectory.getName();
		mIsDirectory = pDirectory.isDirectory();
		mLastModified = new Date(pDirectory.lastModified());
	}

	/**
	 * @param pListGDocs
	 * @return
	 */
	public GDocsFile(List<GDocsFile> pListGDocs) {
		setListGDocs(pListGDocs);
	}

	private List<GDocsFile> mListGDocs;

	/**
	 * @param pFile
	 * @return
	 */
	public GDocsFile(File pFile) {
		mName = StringUtils.substring(pFile.getName(), 0, StringUtils
				.lastIndexOf(pFile.getName(), "."));
		mIsDirectory = pFile.isDirectory();
		mLastModified = new Date(pFile.lastModified());
	}

	/**
	 * @param pDocumentListEntry
	 * @return
	 */
	public GDocsFile(DocumentListEntry pDocumentListEntry) {
		mName = pDocumentListEntry.getTitle().getPlainText();
		for (Category lCategory : pDocumentListEntry.getCategories()) {
			if (StringUtils.equalsIgnoreCase(lCategory.getLabel(),
					FOLDER_CATEGORY)) {
				mIsDirectory = true;
			}
		}
		mEntryServer = pDocumentListEntry;
		mLastModified = new Date(pDocumentListEntry.getUpdated().getValue());
	}

	/**
	 * @return
	 */
	public String getName() {
		return mName;
	}

	/**
	 * @param pListFiles
	 * @return
	 */
	public static List<GDocsFile> FileToGFile(File[] pListFiles) {
		List<GDocsFile> lList = new ArrayList<GDocsFile>();
		for (File lFile : pListFiles) {
			lList.add(new GDocsFile(lFile));
		}
		return lList;
	}

	/**
	 * @return
	 */
	public DiffType getDiffType() {
		return mDiffType;
	}

	/**
	 * @param pDiffType
	 */
	public void setDiffType(DiffType pDiffType) {
		mDiffType = pDiffType;
	}

	/**
	 * @param pObj
	 * @return
	 */
	@Override
	public boolean equals(Object pObj) {
		return new EqualsBuilder().append(this.getName(),
				((GDocsFile) pObj).getName()).isEquals();
	}

	/**
	 * @return
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getName()).hashCode();
	}

	/**
	 * @return
	 */
	public String getPath() {
		return mPath;
	}

	/**
	 * @return
	 */
	public List<GDocsFile> getListGDocs() {
		return mListGDocs;
	}

	/**
	 * @param pPath
	 */
	public void setPath(String pPath) {
		mPath = pPath;
	}

	/**
	 * @param pListGDocs
	 */
	public void setListGDocs(List<GDocsFile> pListGDocs) {
		mListGDocs = pListGDocs;
	}

	/**
	 * @return
	 */
	public boolean isDirectory() {
		return mIsDirectory;
	}

	/**
	 * @return
	 */
	public DocumentListEntry getEntryServer() {
		return mEntryServer;
	}

	/**
	 * @param pEntryServer
	 */
	public void setEntryServer(DocumentListEntry pEntryServer) {
		mEntryServer = pEntryServer;
	}

	/**
	 * @return
	 */
	public String getAbsolutePath() {
		return mAbsolutePath;
	}

	/**
	 * @param pAbsolutePath
	 */
	public void setAbsolutePath(String pAbsolutePath) {
		mAbsolutePath = pAbsolutePath;
	}

	/**
	 * @return
	 */
	public Date getLastModified() {
		return mLastModified;
	}

	/**
	 * @return
	 */
	public int countNbModif() {
		int lNb = 0;
		for (GDocsFile lDocsFile : this.getListGDocs()) {
			if (lDocsFile.isDirectory()) {
				lNb += lDocsFile.countNbModif();
			} else {
				if (!DiffType.EXIST.equals(lDocsFile.mDiffType)) {
					lNb++;
				}
			}
		}
		return lNb;

	}

}
